﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace TVShow.Framework
{
  public class PositionManager
  {

    List<PositionChild> Children = new List<PositionChild>();

    public PositionManager(Control ParentWindow) {
      PositionChild.ParentWindow = ParentWindow;
    }

    public void Add(Control control) {
      Children.Add(new PositionChild(control));
    }

    public void Resize() {
      foreach (PositionChild child in Children) {
        child.Resize();
      }
    }
  }

  class PositionChild
  {
    private Control control;
    public RectangleF RelativeBounds;
    public static Control ParentWindow;

    public PositionChild(Control control) {
      this.Control = control;
    }

    public Control Control {
      get { return this.control; }
      set {
        this.control = value;
        SetRelativeSize();
      }
    }

    public void SetRelativeSize() {
      RelativeBounds = new RectangleF(
        (float)control.Left / ParentWindow.ClientRectangle.Width,
        (float)control.Top / ParentWindow.ClientRectangle.Height,
        (float)control.Width / ParentWindow.ClientRectangle.Width,
        (float)control.Height / ParentWindow.ClientRectangle.Height);
    }

    public void Resize() {
      control.Left = (int)(ParentWindow.ClientRectangle.Width * RelativeBounds.Left);
      control.Top = (int)(ParentWindow.ClientRectangle.Height * RelativeBounds.Top);
      control.Width = (int)(ParentWindow.ClientRectangle.Width * RelativeBounds.Width);
      control.Height = (int)(ParentWindow.ClientRectangle.Height * RelativeBounds.Height);
    }
  }

}
